using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Data;

public partial class Controls_BulkTransfer : Dimok.Web.UI.UserControlEx {
	int QueryStringBatchID { get { return int.Parse("0" + Request["BatchID"]); } }
	protected void Page_Load(object sender, EventArgs e) {
		if (!IsPostBack) {
			btnAddToBatch.Visible = false;
			if (QueryStringBatchID > 0) {
				PurchaseBatches.BatchID = QueryStringBatchID;
				PurchaseContracts.SelectFilter = "PurchaseBatchID=" + QueryStringBatchID;
				PurchaseContracts.SetFilterBoxes(PurchaseContracts.SelectFilter);
				ltrWelcomeJon.Visible = true;
			}
		}
		selStateFrom.SelectedIndexChanged += new EventHandler(selStateFrom_SelectedIndexChanged);
		selStateTo.SelectedIndexChanged += new EventHandler(selStateTo_SelectedIndexChanged);
		selStateTo.DataBound += new EventHandler(selStateTo_DataBound);
		selContractHolders.SelectedIndexChanged += new EventHandler(selContractHolders_SelectedIndexChanged);
		dsStatus.Selected += new ObjectDataSourceStatusEventHandler(dsStatus_Selected);
		dsCH.Selected += new ObjectDataSourceStatusEventHandler(dsCH_Selected);
		PurchaseBatches.RowSelected += new Dimok.Web.UI.GridViewExEventHandler(PurchaseBatches_RowSelected);
		PurchaseBatches.RowDeleted += new GridViewDeletedEventHandler(PurchaseBatches_RowDeleted);
	}

	void selContractHolders_SelectedIndexChanged(object sender, EventArgs e) {
		PurchaseBatches.ContractHolderID =  int.Parse(((ListControl)sender).SelectedValue);
	}

	void PurchaseBatches_RowDeleted(object sender, GridViewDeletedEventArgs e) {
		PurchaseBatches.SelectFilter = "";
		PurchaseBatches.SetFilterBoxes("");
	}

	void PurchaseBatches_RowSelected(object sender, Dimok.Web.UI.GridViewExEventArgs e) {
		int batchID = BatchID;
		btnAddToBatch.Visible = batchID > 0;
		if (btnAddToBatch.Visible) btnAddToBatch.Text =
			"Add to [" + PurchaseBatches.GridViewEx.SelectedValues["ID"] + "]";// +PurchaseBatches.GridViewEx.SelectedValues["Name"] + "]";
		btnNewBatch.Visible = batchID == 0;
	}

	private int BatchID { get { return PurchaseBatches.SelectedIndex >= 0 ? int.Parse("0"+PurchaseBatches.GridViewEx.SelectedValues["ID"]) : 0; } }
	protected override void OnPreRender(EventArgs e) {
		base.OnPreRender(e);
	}

	void dsCH_Selected(object sender, ObjectDataSourceStatusEventArgs e) {
		DataView dv = (DataView)e.ReturnValue;
		int chID = int.Parse(dv[0]["ContractHolderID"]+"");
		if (chID < 0) PurchaseContracts.ContractHolderIDRemove();
		else PurchaseContracts.ContractHolderID = chID;
	}

	void dsStatus_Selected(object sender, ObjectDataSourceStatusEventArgs e) {
		DataView dv = (DataView)e.ReturnValue;
		int statusID = dv.Count > 0 ? int.Parse("0" + dv[0]["StatusID"]) : 0;
		if (statusID > 0) PurchaseContracts.StatusID = statusID;
		else PurchaseContracts.StatusIDRemove();
	}

	protected void MakeBatch(object sender, EventArgs e) {
		try {
			string filter;
			if (PurchaseContracts.SelectedIndex >= 0)
				filter = "LoanID=" + PurchaseContracts.GV.SelectedValue;
			else {
				if (selStateFrom.SelectedIndex <= 0) throw new Exception("Must select status.");
				filter = PurchaseContracts.GridViewEx.BuildFilter();
				string status = "StatusID=" + selStateFrom.SelectedValue;
				filter = status + (filter == "" ? "" : " AND " + filter);
			}
			if (selContractHolders.SelectedIndex > 0) filter += " AND ContractHolderID=" + selContractHolders.SelectedValue;
			else throw new Exception("Must select a Contract Holder.");
			int batchID = (int)SQL.ExecuteSP("s_PurchaseContract_CreateBatch",int.Parse(selContractHolders.SelectedValue), txtBatchName.Value, txaBatchDesc.Value, filter,BatchID);
			PurchaseBatches.BatchID = batchID;
		} catch (Exception exc) { throw new Dimok.Exceptions.ASPException("",exc); }
	}

	void selStateTo_SelectedIndexChanged(object sender, EventArgs e) {
		int statusFrom = int.Parse(selStateFrom.SelectedValue);
		int statusTo = int.Parse(selStateTo.SelectedValue);
		if( statusTo == statusFrom )throw new Exception("Status from must be different from Status to.");
		List<string> lstFilter = new List<string>();
		string filter = PurchaseContracts.GridViewEx.BuildFilter();
		if (filter != "") lstFilter.Add(filter);
		if (selContractHolders.SelectedIndex > 0) lstFilter.Add("ContractHolderID=" + selContractHolders.SelectedValue);
		SQL.ExecuteSP(60 * 5, "s_PurchaseContract_SendBulk", statusFrom, statusTo, string.Join(" AND ", lstFilter.ToArray()));
		selStateFrom.SelectedValue = statusTo+"";
		StatusFrom = statusTo;
	}

	void selStateTo_DataBound(object sender, EventArgs e) {
		try {
			if (int.Parse(selStateFrom.SelectedValue) > 0) {
				selStateTo.SelectedValue = selStateFrom.SelectedValue;
				selStateTo.Attributes["stateFrom"] = selStateFrom.SelectedValue;
			} else PurchaseContracts.StatusIDRemove();
		} catch (Exception exc) { throw new Dimok.Exceptions.ASPException(exc); }
	}

	void selStateFrom_SelectedIndexChanged(object sender, EventArgs e) {
		ListControl ddl = sender as ListControl;
		StatusFrom = ddl.SelectedValue;
	}
	object StatusFrom {
		set {
			dsStatesNext.StateFromID = int.Parse("0" + value);
		}
	}
}
